package tk.heimashuma.store.web.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import tk.heimashuma.store.entity.Addrs;
import tk.heimashuma.store.entity.Transaction;
import tk.heimashuma.store.entity.Users;
import tk.heimashuma.store.entity.dto.AddAddr;
import tk.heimashuma.store.entity.dto.State;
import tk.heimashuma.store.service.UserInfoService;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Controller
@RequestMapping("/user/info")
public class UsersInfoController {
    @Autowired
    private  UserInfoService userInfoService;
    @GetMapping("/show")
    public String showInfo(HttpSession session, Map<String,Object> map){
        Object o = session.getAttribute("user");
        Users user = (Users) o;
        map.put("info",user);
        return "/info/Personal_information";
    }
    @GetMapping("/show/addr")
    public String showAddr(HttpSession session,Map<String,Object> map){
        Object o = session.getAttribute("user");
        Users users = (Users) o;
        // 再次查询
        List<Addrs> addrsList = userInfoService.findAddrByUser(users);
        map.put("info",addrsList);
        return "/info/user_address";
    }
    @PostMapping("/add/addr")
    public @ResponseBody State UserAddAddr(Addrs addrs, HttpSession session){
        Object o = session.getAttribute("user");
        Users users = (Users) o;
        userInfoService.addAddrByUser(addrs,users);
        AddAddr addAddr = new AddAddr();
        addAddr.setAid(addrs.getAid());
        addAddr.setCode(200);
        addAddr.setMsg("ok");
        return addAddr;
    }
    @PostMapping("/show/addr/del")
    public @ResponseBody State UserAddDel(Integer aid,HttpSession session){
        Object o = session.getAttribute("user");
        Users users = (Users) o;
        boolean f = userInfoService.delAddr(aid,users);
        State state = new State();
        if(f){
            state.setCode(200);
            state.setMsg("ok");
        }else{
            state.setCode(500);
            state.setMsg("error");
        }
        return state;
    }
    @GetMapping("/show/orders/all")
    public String toAllOrder(Map<String,Object> map,HttpSession session){
        List<Transaction> list = getTransactions("all", 1, 10,session);
        map.put("info",list);
        return "/info/All_orders";
    }
    @GetMapping("/show/orders/unpaid")
    public String toUnpaidOrder(Map<String,Object> map,HttpSession session){
        List<Transaction> list = getTransactions("unpaid", 1, 10,session);
        map.put("info",list);
        return "/info/pending_payment";
    }
    @GetMapping("/find/orders")
    public @ResponseBody List<Transaction> getTransactions(String type,Integer page,Integer pageSize,HttpSession session){
        Object o = session.getAttribute("user");
        Users user = (Users) o;
        if(!Optional.ofNullable(type).isPresent()) type ="all";
        List<Transaction> list = null;
        if("all".equals(type)){
            list= userInfoService.findTransactions(page,pageSize,user);
            return list;
        }else if("unpaid".equals(type)){
            list = userInfoService.findConditionTransactions(page,pageSize,user, 3);
            return list;
        }
        return null;
    }
}
